package com.seele.tigerwallet.ui.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

@SuppressLint("AppCompatCustomView")
public class SearchEditext extends EditText implements View.OnTouchListener {

    private OnClickIconListener listener;

    public interface OnClickIconListener {
        void onClcik();

    }

    public void setOnClickIconListener(OnClickIconListener listener) {
        this.listener = listener;
    }

    public SearchEditext(Context context) {
        super(context);
        setOnTouchListener(this);
    }

    public SearchEditext(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnTouchListener(this);
    }

    public SearchEditext(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnTouchListener(this);
    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {


        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {
                boolean touchable = motionEvent.getX() > (getWidth() - getTotalPaddingRight())
                        && (motionEvent.getX() < ((getWidth() - getPaddingRight())));

                if (touchable) {
                    //里面写上自己想做的事情，也就是DrawableRight的触发事件
                    ;
                    try {
                        if (null != listener) {
                            listener.onClcik();
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                    }

                }
            }
        }


//        //如果不是按下操作，就不做处理，如果是按下操作但是没有图片，也不做处理
//        if (motionEvent.getAction() == MotionEvent.ACTION_UP && this.getCompoundDrawables()[2] != null) {
//            //检测点击区域的X坐标是否在图片范围内
//            if (motionEvent.getX() > this.getWidth() - this.getPaddingRight() - this.getCompoundDrawables()[2].getIntrinsicWidth()) {
//
//
//                if (null != listener) {
//                    listener.onClcik();
//                }
//            }
//            return false;
//        }
        return super.onTouchEvent(motionEvent);

    }


    /**
     * @param editText
     */
    public  void setEditTextInhibitInputSpace(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ")) {
                    return "";
                }
                return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }
}
